package net.wang.concurrency.test.commonUnsafe;

import lombok.extern.slf4j.Slf4j;
import net.wang.concurrency.annoations.ThreadSafe;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;


@SuppressWarnings("ALL")
@ThreadSafe
@Slf4j
public class DateFormatExample2 {

    public static int clientTotal = 500;
    public static int threadTotal = 200;


    @Test
    public void notThreadSafeTest() throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        for (int i = 0; i < clientTotal; i++) {
            executorService.execute(() -> {
                try {
                    semaphore.acquire();

                    add();

                    semaphore.release();
                } catch (InterruptedException e) {
                    log.info("exception : {}", e);
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();

    }

    public void add() {
        //LocalDate localDate = LocalDate.parse("20180812");

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        try {
            format.parse("20180812");
        } catch (ParseException e) {
            log.info(e.getMessage());
        }
    }

}
